<?php

/**
 * Created by PhpStorm.
 * User: CCA
 * Date: 2019/10/10
 * Time: 9:38
 */
namespace app\api;
use app\common\model\Member;
use app\common\model\Pointcart;
use app\common\model\Pointorder;
use app\common\model\Token;
use app\common\model\Voucher;
use app\home\controller\BaseMall;
use think\Controller;
use think\Request;


class BaseController extends BaseMall
{

    public $user_info = null;
    public $user_id = null;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);

        \houdunwang\config\Config::single()->set('app.key',config('app.key'));

        $white_controller = ['home','goods','test','building']; //控制器免登陆白名单
        $white_methods = ['login','register','inspect_app_edition','service_tips']; //方法免登陆白名单
        $tokenModel = new Token();
        $memberModel = Member::getInstance();
        $methods = $request->action();
        $controller = $request->controller();
        if(!in_array($methods,$white_methods)&&!in_array(strtolower($controller),$white_controller)){
            if(!($token = input('token'))){
                return json_fail_return('缺少token');
            }
            $tokenInfo = $tokenModel->getToken(['token'=>$token]);
            if(!$tokenInfo){
               return json_fail_return('token验证失败');
            }
            if($tokenInfo['expire_time']<time()){
                return json_fail_return('token已过期');
            }
            $this->user_id = $tokenInfo['user_id'];
            $member_info = $memberModel->getMemberInfoByID($this->user_id);
            $memberModel->user_id = $this->user_id;

            if ($member_info) {
                $member_gradeinfo = $memberModel->getOneMemberGrade(intval($member_info['member_exppoints']));
                $this->user_info = array_merge($member_info, $member_gradeinfo);
                $memberModel->user_info = $this->user_info;
            }
        }
    }

    /**
     * 获得积分中心会员信息包括会员名、ID、会员头像、会员等级、经验值、等级进度、积分、已领代金券、已兑换礼品、礼品购物车
     */
    public function pointshopMInfo() {
        if ($this->user_info) {
            $member_model = new Member();
            $member_infotmp = $member_model->getMemberInfoByID($this->user_id);
            $member_infotmp['member_exppoints'] = intval($member_infotmp['member_exppoints']);

            //当前登录会员等级信息
            $membergrade_info = $member_model->getOneMemberGrade($member_infotmp['member_exppoints'], true);
            $member_info = array_merge($member_infotmp, $membergrade_info);

            //查询已兑换并可以使用的代金券数量
            $voucher_model =new Voucher();
            $vouchercount = $voucher_model->getCurrentAvailableVoucherCount(session('member_id'));

            //购物车兑换商品数
            $pointcart_count = (new Pointcart())->getPointcartCount(session('member_id'));

            //查询已兑换商品数(未取消订单)
            $pointordercount = (new Pointorder())->getMemberPointsOrderGoodsCount(session('member_id'));

            return array('member_info' => $member_info, 'vouchercount' => $vouchercount, 'pointcart_count' => $pointcart_count, 'pointordercount' => $pointordercount);
        }
    }

    protected function buildAccessToken($lenght = 32)
    {
        //生成AccessToken
        $str_pol = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789abcdefghijklmnopqrstuvwxyz";
        return substr(str_shuffle($str_pol), 0, $lenght);

    }

}